package by.exposit.service;

import by.exposit.dao.ProjectDAO;
import by.exposit.dao.UserDAO;
import by.exposit.dao.entity.FieldEntity;
import by.exposit.dao.entity.ProjectEntity;
import by.exposit.dao.entity.UserEntity;
import by.exposit.domain.Field;
import by.exposit.domain.Project;
import by.exposit.domain.User;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

    @Service
    @Transactional
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectDAO projectDAO;

    @Autowired
    private UserDAO userDAO;


    @Override
    public void saveProject(Project project)
    {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        projectDAO.save(mapper.map(project,ProjectEntity.class));
    }

    @Override
    public List<Project> projectList() {

        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
              List<Project> projects = new ArrayList<Project>();
        List<ProjectEntity> projectEntities = projectDAO.getAll();
        for(ProjectEntity project:projectEntities)
        {
            projects.add(mapper.map(project,Project.class));
        }
        return projects;
    }

    @Override
    public List<Project> projectFieldList(Field field) {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        List<Project> projects = new ArrayList<Project>();
        List<ProjectEntity> projectEntities = projectDAO.projectFieldList(mapper.map(field,FieldEntity.class));
        for(ProjectEntity project:projectEntities)
        {
            projects.add(mapper.map(project,Project.class));
        }
        return projects;
    }

    @Override
    public List<Project> projectUserList(Long userId) {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        List<Project> projects = new ArrayList<Project>();
        UserEntity user = userDAO.findByID(userId);
        List<ProjectEntity> projectEntities = projectDAO.projectUserList(user);
        for(ProjectEntity project:projectEntities)
        {
            projects.add(mapper.map(project,Project.class));
        }
        return projects;

    }

    @Override
    public List<Project> projectUserWantToFinanceList(Long userId) {
        return null;
    }

    @Override
    public void remove(Long projectId) {

      projectDAO.remove(projectId);


    }

    @Override
    public Project retrieveProject(Long projectId) {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        return mapper.map(projectDAO.findByID(projectId),Project.class);
    }

    public void changeState(Project project, float state)
    { projectDAO.changeState(project,state);}

    @Override
    public void isWantToHelp(Long projectId) {
        projectDAO.isWantToHelp(projectId);
    }

        @Override
        public List<Field> getFieldsInUsersProjects(User user) {
            Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
            List<FieldEntity> fieldEntities =projectDAO.getFieldsInUsersProjects(mapper.map(user,UserEntity.class));
            List<Field> fields = new ArrayList<Field>();
            for(FieldEntity fieldEntity: fieldEntities)

            fields.add(mapper.map(fieldEntity,Field.class));
                return fields;
        }
}